﻿
using System ;
using System.Linq ;

using MskEdit.Msk.Representation ;


namespace MskEdit.Msk.Model
{

    public class MskLanguage
    {
        
#region Private

        private static string[]     validLanguageCodes_ = new [] { "GB", "D", "S" } ;
        private static MskLanguage  nullLanguage_       = new MskLanguage() ;
        private        string       languageCode_ ;


        private static bool IsValidLanguageCode( string languageCodeAsString ) {
        
            if ( string.IsNullOrEmpty( languageCodeAsString ) )
                return false ;
                
            if ( ! validLanguageCodes_.Contains( languageCodeAsString.Trim() ) )
                return false ;
                
            return true ;
        }


        private MskLanguage() {
            languageCode_ = StringField.Language.TryFrom( "", "" ) ;
        }


        private MskLanguage( string languageCode ) {
            languageCode_ = StringField.Language.TryFrom( "", languageCode ) ;
        }

#endregion

#region Public 
 
 
        public static MskLanguage TryFrom( string line, MskLanguage fallbackValue ) { 
        
            string languageStr = StringField.Language.TryFrom( line, null ) ;
            
            if ( languageStr == null )
                return fallbackValue ;
                
            if ( ! MskLanguage.IsValidLanguageCode( languageStr ) )
                return fallbackValue ;
                
            return new MskLanguage( languageStr ) ;
        }
    
    
        public bool Matches( String line ) {
        
            MskLanguage mskLang = MskLanguage.TryFrom( line, null ) ;
            
            if ( mskLang == null )
                return false ;
                
            if ( this.languageCode_ != mskLang.languageCode_ )
                return false ;
                
            return true ;
        }
 
 
        public override string ToString() {
            return languageCode_.Trim() ;
        }


        public override bool Equals( object obj ) {
        
            MskLanguage that = ( MskLanguage ) obj ;
            
            if ( ! that.languageCode_.Equals( this.languageCode_ ) )
                return false ;
                
            return true ;
        }
            
            
        public override int GetHashCode() {
            int hashCode = languageCode_.GetHashCode() ;
            return hashCode ;
        }


        public static MskLanguage NullLanguage {
            get { return nullLanguage_ ; }
        }

        
#endregion

    }
}
